//
//  HomeViewController.swift
//  yundcmHD
//
//  Created by qmsoft_macmini on 16/2/22.
//  Copyright © 2016年 qmsoft_macmini. All rights reserved.
//

import UIKit

let screenW = Utils.screenWidth()
let screenH = Utils.screenHeight()

class HomeViewController: UIViewController, UIScrollViewDelegate {
    @IBOutlet weak var sc_tab: UISegmentedControl!
    @IBOutlet weak var sv_home: UIScrollView!
    
    
    var studyVC:StudyViewController!
    var historyVC:HistoryViewController!
    var collectionVC:CollectionViewController!
    var viewAyy:[UIView]!

    override func viewDidLoad() {
        super.viewDidLoad()
        viewAyy = [UIView]()
        studyVC = storyboard?.instantiateViewControllerWithIdentifier("studyID") as! StudyViewController
        viewAyy.append(studyVC.view)
        historyVC = storyboard?.instantiateViewControllerWithIdentifier("historyID") as! HistoryViewController
        viewAyy.append(historyVC.view)
        collectionVC = storyboard?.instantiateViewControllerWithIdentifier("collectionID") as! CollectionViewController
        viewAyy.append(collectionVC.view)
        sv_home.frame = CGRectMake(0, 0, screenW, screenH)
        for var i=0;i<viewAyy.count;i++ {
            viewAyy[i].frame = CGRectMake(CGFloat(i) * screenW, 0, screenW, sv_home.frame.height)
            sv_home.addSubview(viewAyy[i])
        }
        sv_home.contentSize = CGSizeMake(screenW * CGFloat(viewAyy.count), 0)
        sv_home.delegate = self
        sc_tab.selectedSegmentIndex = 0
    }
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
        if scrollView == sv_home {
            let _currPage = (scrollView.contentOffset.x + scrollView.frame.width * 0.6) / scrollView.frame.width
            sc_tab.selectedSegmentIndex = Int(_currPage)
        }
    }
    
    @IBAction func onChangedTab(sender: UISegmentedControl) {
        sv_home.contentOffset = CGPointMake(screenW * CGFloat(sender.selectedSegmentIndex), 0)
    }

}
